const connect = require("../model");
// const config = require("../config")
const order = {
    async add(req, res) {
        let {
            orderSn,
            price,
            status
        } = req.body


        //  查询是否重复
        let sql2 = "select * from  `order` where orderSn = ? "
        let [data] = await connect.query(sql2, [orderSn])
        if (data.length != 0) {
            res.json({
                code: 40001,
                message: "订单已重复"
            })
            return
        }

        var sql = ""
        if (status) {
            sql = "INSERT into `order`(orderSn,price,status,userid) VALUES(?,?,?,?)"
            await connect.query(sql, [orderSn, price, 1, req.session.id])
        } else {
            sql = "INSERT into `order`(orderSn,price,status) VALUES(?,?,?)"
            await connect.query(sql, [orderSn, price, 0])
        }
        res.json({
            code: 2000,
            messsage: "成功发布"
        })
    },
    async getorder(req, res) {
        let { page, size } = req.body
        page = (page - 1) * size
        let sql = 'select * from `order` WHERE  `status` = 0  LIMIT  ? , ?; select COUNT(*) as total from `order` WHERE  `status` = 0 ;'
        let [data] = await connect.query(sql, [page, size - 0])

        res.json({
            code: 2000,
            data: data[0],
            total: data[1][0].total
        })
    },

    async jiedan(req, res) {
        let { id } = req.body
        let userid = req.session.id
        let sql = "UPDATE `order`  SET userid = ?, status = 1  WHERE  id = ? and  userid  is null;"
        let [data] = await connect.query(sql, [userid, id])

        if (data.affectedRows == 1) {
            res.json({
                code: 2000,
                message: "接单成功"
            })
        } else {
            res.json({
                code: 40002,
                message: "接单失败"
            })
        }

    },
    async myself(req, res) {
        let { orwhere = [2], keyword = "", page = 1, size = 10 } = req.body
        page = (page - 1) * size
        let id = req.session.id
        //         `SELECT A.*  , JSON_ARRAYAGG(JSON_OBJECT('link', B.link, 'type', B.type)) AS Items 
        // FROM \`order\` as A  left  JOIN OrderStatic as B ON A.id = B.orderid   WHERE A.userid = 5 
        // and A.orderSn like '%%' 
        // and ( A.\`status\` = 1 )       
        // GROUP BY A.id   `
        let sql = "SELECT A.*  , JSON_ARRAYAGG(JSON_OBJECT('link', B.link, 'type', B.type)) AS Items  FROM `order` as A  left  JOIN OrderStatic as B ON A.id = B.orderid  "
        var wherestr = " WHERE A.userid = ? and A.orderSn like '%" + keyword + "%' "
        sql = sql + wherestr
        var str = ""
        if (orwhere.length > 0) {
            str = str + "and ( "
            orwhere.forEach((v, i) => {
                if (i == 0) {
                    str = str + "A.`status` = " + v + " "
                } else {
                    str = str + "or  A.`status` = " + v + " "
                }
            })
            str += ")"
        }

        sql = sql + str + " GROUP BY A.id  LIMIT ? ,?"
        sql = sql + ";select COUNT(*) as total from `order` as A " + wherestr + str



        // let sql = "SELECT A.*  , JSON_ARRAYAGG(JSON_OBJECT('link', B.link, 'type', B.type)) AS Items FROM `order` as A Left JOIN OrderStatic as B ON A.id = B.orderid  WHERE A.userid = ?  GROUP BY A.id LIMIT ?,? "
        let [data] = await connect.query(sql, [id, page, size - 0, id])
        res.json({
            code: 2000,
            data: data[0],
            total: data[1][0].total
        })
    }
    ,
    async list(req, res) {
        let { orwhere = [2], keyword = "", page = 1, size = 10 } = req.body
        page = (page - 1) * size
        let id = req.session.id
        //         `SELECT A.*  , JSON_ARRAYAGG(JSON_OBJECT('link', B.link, 'type', B.type)) AS Items 
        // FROM \`order\` as A  left  JOIN OrderStatic as B ON A.id = B.orderid   WHERE A.userid = 5 
        // and A.orderSn like '%%' 
        // and ( A.\`status\` = 1 )       
        // GROUP BY A.id   `
        let sql = "SELECT A.* ,   (SELECT JSON_ARRAYAGG(JSON_OBJECT('name' ,c.name , 'avatar',c.avatar , 'payimg',c.payimg )) FROM userinfo as C WHERE C.id = A.userid) AS userinfo ,JSON_ARRAYAGG(JSON_OBJECT('link', B.link, 'type', B.type)) AS Items  FROM `order` as A  left  JOIN OrderStatic as B ON A.id = B.orderid  "
        var wherestr = " WHERE  A.orderSn like '%" + keyword + "%' "
        sql = sql + wherestr
        var str = ""
        if (orwhere.length > 0) {
            str = str + "and ( "
            orwhere.forEach((v, i) => {
                if (i == 0) {
                    str = str + "A.`status` = " + v + " "
                } else {
                    str = str + "or  A.`status` = " + v + " "
                }
            })
            str += ")"
        }

        sql = sql + str + " GROUP BY A.id  LIMIT ? ,?"
        sql = sql + ";select COUNT(*) as total from `order` as A " + wherestr + str



        // let sql = "SELECT A.*  , JSON_ARRAYAGG(JSON_OBJECT('link', B.link, 'type', B.type)) AS Items FROM `order` as A Left JOIN OrderStatic as B ON A.id = B.orderid  WHERE A.userid = ?  GROUP BY A.id LIMIT ?,? "
        let [data] = await connect.query(sql, [page, size - 0, id])
        res.json({
            code: 2000,
            data: data[0],
            total: data[1][0].total
        })
    }
    ,
    async Orderstatic(req, res) {
        let { orderid, status } = req.body
        let id = req.session.id
        let sql = "UPDATE `order` SET  `status` = ? WHERE userid = ?  and id = ?"
        let [data] = await connect.query(sql, [status, id, orderid])
        if (data.affectedRows == 1) {
            res.json({
                code: 2000,
                message: "修改成功"
            })
        } else {
            res.json({
                code: 40001,
                message: "无权限修改"
            })
        }

    },
    async Orderstatic2(req, res) {
        let { orderid } = req.body
        let id = req.session.id
        let sql = "UPDATE `order` SET  `status` = 0  , userid = null  WHERE userid = ?  and id = ?"
        let [data] = await connect.query(sql, [id, orderid])
        if (data.affectedRows == 1) {
            res.json({
                code: 2000,
                message: "修改成功"
            })
        } else {
            res.json({
                code: 40001,
                message: "无权限修改"
            })
        }

    }
    ,
    async orderfile(req, res) {
        // 获取订单文件
        let { orderid } = req.body

        var sql = "select  * from orderstatic  where orderid = ?"
        let [data] = await connect.query(sql, [orderid])
        res.json({
            code: 2000,
            data: data
        })


    }
    ,
    async orderpay(req, res) {
        let { orderid, url } = req.body
        let sql = "update `order` set img = ? ,status = 4 where id =?"
        await connect.query(sql, [url, orderid])
        res.json({
            code: 2000,
            message: "修改成功"
        })


    }
}


module.exports = order